package com.example.sistemacompras.adapter;

import java.util.List;

import com.example.sistemacompras.R;
import com.example.sistemacompras.entity.Product;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductListAdapter extends ArrayAdapter<Product> {
	private int textViewResourceId;

    private List<Product> objects;

    public ProductListAdapter(Context context, int textViewResourceId, List<Product> objects) {
        super(context, textViewResourceId, objects);
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(textViewResourceId, null);
        Product prd = objects.get(position);
        if (prd != null) {
            ImageView image = (ImageView) row.findViewById(R.id.product_image);
            image.setImageBitmap(prd.getPhoto());

            TextView itemName = (TextView) row.findViewById(R.id.product_name);
            itemName.setText(prd.getName());

            TextView itemPrice = (TextView) row.findViewById(R.id.product_price);
            itemPrice.setText(itemPrice.getText() + " " + String.valueOf(prd.getUnitValue()));
        }

        return row;
    }
}
